package cn.hh.app;

import cn.hh.annotation.MyAfter;
import cn.hh.annotation.MyBefore;
import cn.hh.annotation.MyTest;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;

/**
 * @Author huangh
 * @date 2021/11/02
 * @Description: 山寨Junit
 */
public class MyJunitFrameWork {
    public static void main(String[] args) throws IllegalAccessException, InstantiationException, InvocationTargetException {
        //1.找到测试类的字节码，EmployeeDaoTest
        Class clazz = EmployeeDaoTest.class;
        Object obj = clazz.newInstance();

        //2.获取EmployeeDaoTest类中所有公用方法
        Method[] methods = clazz.getMethods();

        //3.迭代出每个Method对象，判断上面使用的注解方法
        List<Method> myBeforeList = new ArrayList<>();
        List<Method> myAfterList = new ArrayList<>();
        List<Method> myTestList = new ArrayList<>();
        for (Method method:methods){
            if(method.isAnnotationPresent(MyBefore.class)){
                myBeforeList.add(method);
            }else if(method.isAnnotationPresent(MyAfter.class)){
                myAfterList.add(method);
            }else if(method.isAnnotationPresent(MyTest.class)){
                myTestList.add(method);
            }
        }

        for (Method testMethod : myTestList){
            //先执行@MyBefore方法
            for (Method beforeMethod : myBeforeList){
                beforeMethod.invoke(obj);
            }
            testMethod.invoke(obj);
            //先执行@MyAfter方法
            for (Method afterMethod : myAfterList){
                afterMethod.invoke(obj);
            }
        }

    }
}
